<template>
  <div class="login-box">
    <div class="login-container">
      <div class="title-container">
        <img src="../../assets/log2.png" alt="">
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left" style="padding:40px">

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入内容"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-checkbox v-model="checked">记住密码</el-checkbox>
        <el-button :loading="loading" type="warning" style="width:100%;margin:10px 0;" @click.native.prevent="handleLogin">立即登录</el-button>
      </el-form>

    </div>
    <div class="login-logo" />
  </div>

</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入6-16个字符'))
      } else {
        callback()
      }
    }
    return {
      checked: false,
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword },
          { min: 6, max: 16, message: '请输入6-16个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.commit('user/SET_TOKEN', 111111)
          this.$router.push({ path: this.redirect || '/' })
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  // .el-form-item {
  //   border: 1px solid rgba(255, 255, 255, 0.1);
  //   // background: rgba(0, 0, 0, 0.1);
  //   // background: #f8f5f5;
  //   border-radius: 5px;
  //   color: #454545;
  // }
}
</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#ccc;

body{
  position: relative;
}
// 登录盒子
.login-box {
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  width: 958px;
  height: 516px;
  border-radius: 40px;
  box-shadow: 0 0 20px rgb(93 93 93 / 33%);
}
.login-logo {
  right: 0;
  background-image: url(../../assets/log1.png);
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  width: 560px;
  height: 516px;
}
.el-form-item__content{
  border: 1px solid rgba(255, 255, 255, 0.1);
  background-color: #fff;
  border-radius: 5px;
}

.login-container {
  float: left;
  border-radius: 34px 0 0 34px;
  min-height: 100%;
  width: 398px;

  .login-form {
    position: relative;
    margin: 0 auto;
    overflow: hidden;
  }

  .el-form-item {
    margin-top: 35px;
  }
  .svg-container {
    background-color: #ccc;
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 40px;
    height: 47px;
    display: inline-block;
  }

  .title-container {
    margin-left: 124px;
    margin-top: 72px;
    img {
    width: 150px;
    height: 64px;
  }

  }

  .show-pwd {
    position: absolute;
    right: 15px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
